package utils

import (
	"mime/multipart"
	"net/http"
	"path/filepath"
	"strings"
)

func GetFilePreImg(fileType string, absolutePath string) string {
	if strings.Contains(fileType, "image") {
		return absolutePath
	}
	return ""
}

func GetFileInfo(fileHeader *multipart.FileHeader) (fileType string, fileName string, suffix string) {
	fileName = fileHeader.Filename
	suffix = filepath.Ext(fileName)
	// 打开文件内容（注意是 multipart.File）
	file, _ := fileHeader.Open()
	defer file.Close()
	buf := make([]byte, 512)
	_, _ = file.Read(buf)
	fileType = http.DetectContentType(buf)
	if fileType == "application/octet-stream" || fileType == "binary/octet-stream" {
		switch strings.ToLower(suffix) {
		case ".jpg", ".jpeg":
			fileType = "image/jpeg"
			break
		case ".png":
			fileType = "image/png"
			break
		case ".gif":
			fileType = "image/gif"
			break
		case ".bmp":
			fileType = "image/bmp"
			break
		case ".webp":
			fileType = "image/webp"
			break
		case ".svg":
			fileType = "image/svg+xml"
			break
		case ".tif", ".tiff":
			fileType = "image/tiff"
			break
		case ".ico":
			fileType = "image/x-icon"
			break

			// 视频格式
		case ".mp4":
			fileType = "video/mp4"
			break
		case ".avi":
			fileType = "video/x-msvideo"
			break
		case ".mov":
			fileType = "video/quicktime"
			break
		case ".wmv":
			fileType = "video/x-ms-wmv"
			break
		case ".flv":
			fileType = "video/x-flv"
			break
		case ".mkv":
			fileType = "video/x-matroska"
			break
		case ".webm":
			fileType = "video/webm"
			break
		case ".mpeg", ".mpg":
			fileType = "video/mpeg"
		case ".ts":
			fileType = "video/MP2T"
			break

		case ".m3u8":
			fileType = "application/vnd.apple.mpegurl"
			break

			// PDF 格式
		case ".pdf":
			fileType = "application/pdf"
			break

		default:
			fileType = suffix
			break
		}
	}
	return
}
